package com.kgc.search.controller;

import com.kgc.search.service.ItemSolrService;
import com.kgc.utils.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SearchController {

    @Autowired
    private ItemSolrService itemSolrService;

    @RequestMapping("search")
    /**
     * 查询条件
     * page  页面号
     */
    public String search(String q,
                         @RequestParam(value = "page",required = false,defaultValue = "1") int page,
                         Model model) throws Exception{
       String queryParam=new String(q.getBytes("iso-8859-1"),"UTF-8");
        //根据q的值来查询solr
        SearchResult searchResult=itemSolrService.queryItemSolr(queryParam,page);
        //将结果返回

           model.addAttribute("query",queryParam);
           model.addAttribute("totalPages",searchResult.getTotalPages());
           model.addAttribute("itemList",searchResult.getItemList());
        return "search";
    }

}
